package org.kjtc.controller;

import org.kjtc.entity.*;
import org.kjtc.mapper.TimerMapper;
import org.kjtc.service.BackLogService;
import org.kjtc.service.LogService;
import org.kjtc.service.LoginService;
import org.kjtc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/21 0021.
 */
@RestController
public class BackLogController extends BaseController {

    @Autowired
    private BackLogService backLogService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private LogService logService;

    @RequestMapping(value = "doToIndex")
    public JsonResult doToIndex(@RequestParam("username") String userName) {
        List<UserInfo> list = loginService.toIndex(userName);
        this.getSession().setAttribute("userInfo", list.get(0));
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetUser")
    public JsonResult doGetUser() {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        return new JsonResult(userInfo);
    }

    @RequestMapping(value = "doGetAllProducts")
    public JsonResult doGetAllProducts() {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        List<Product> list = null;
        try{
            list = backLogService.getAllProducts();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetProductVersions")
    public JsonResult doGetProductVersions(String productId) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        List<ProductVersion> list = null;
        try{
            list = backLogService.getAllProductVersions(productId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetAllMembers")
    public JsonResult doGetAllMembers() {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        List<UserInfo> list = null;
        try{
            list = backLogService.getAllMembers();
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetBackLog")
    public JsonResult doGetBackLog(@RequestParam("productVersionId") String productVersionId, @RequestParam("MemberId") String MemberId, @RequestParam("BacklogStatus") String BacklogStatus) {
        List<BackLog> list = null;
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            list = backLogService.getBackLog(productVersionId, MemberId, BacklogStatus);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetbackLogFeed")
    public JsonResult doGetbackLogFeed(String backLogId) {
        List<Map<String, Object>> list = null;
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            list = backLogService.getBackLogFeed(backLogId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }
    @RequestMapping(value = "doGetCurrentTitle")
    public JsonResult doGetCurrentTitle(@RequestParam("$productVersionId") String productVersionId) {
        List<BackLog> list = backLogService.getCurrentTitle(productVersionId);
        return new JsonResult(list);
    }

    @RequestMapping(value = "updatePunchTime")
    public JsonResult updatePunchTime(@RequestParam("backLogId") String backLogId) {
        int number = 0;
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            number = backLogService.updateActualTime(backLogId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(number);
    }

    @RequestMapping(value = "getPunchEveryDay")
    public JsonResult doGetPunchEveryDay(@RequestParam("backLogId") String backLogId) {
        UserInfo userInfo= (UserInfo) this.getSession().getAttribute("userInfo");
        String userID=userInfo.getUserID();
        int number = 0;
        if (userID != null && userID != "") {
            number = backLogService.getPunchEveryDay(userID, backLogId);
        }
        return new JsonResult(number);
    }

    @RequestMapping(value = "getUserIDByBackLogID")
    public JsonResult doGetUserIDByBackLogID(@RequestParam("backLogId") String backLogId) {
        UserInfo userInfo= (UserInfo) this.getSession().getAttribute("userInfo");
        String userID=userInfo.getUserID();
        int number = 0;
        if (userID != null && userID != "") {
            number = backLogService.getUserIDByBackLogID(userID, backLogId);
        }
        return new JsonResult(number);
    }

    @RequestMapping(value = "doGetFeedByProductVersionId")
    public JsonResult doGetFeedByProductVersionId(@RequestParam("$productVersionId") String productVersionId) {
        List<Map<String, Object>> list = backLogService.getFeedByProductVersionId(productVersionId);
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetBackLogByProductVersionId")
    public JsonResult doGetBackLogByProductVersionId(@RequestParam("$productVersionId") String productVersionId) {
        List<BackLog> list = null;
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            list = backLogService.getBackLogByProductVersionId(productVersionId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doGetBackLogByUser")
    public JsonResult doGetBackLogByUser(BackLog backLog) {
        List<BackLog> list = null;
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            if (backLog.getUserID() == null || backLog.getUserID() == "") {
                String userID = userInfo.getUserID();
                if (userID != null && userID != "") {
                    backLog.setUserID(userID);
                    list = backLogService.getBackLogByUser(backLog);
                }
            } else {
                list = backLogService.getBackLogByUser(backLog);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(list);
    }

    @RequestMapping(value = "doAddPunchRecord")
    public JsonResult doAddPunchRecord(@RequestParam("backLogId") String backLogId) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        int number = 0;
        try{
            if (userID != null && userID != "") {
                number = backLogService.addPunchRecord(userID, backLogId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(number);
    }

    @RequestMapping(value = "doUpdateBackLogState")
    public JsonResult doUpdateBackLogState(@RequestParam("backLogState") String backLogState, @RequestParam("backLogID") String backLogID) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        int number = 0;
        try{
            number = backLogService.updateBackLogState(backLogState, backLogID,userID);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(number);
    }
    @RequestMapping(value = "doUpdateBackLogEndDTTM")
    public JsonResult doUpdateBackLogEndDTTM(@RequestParam("backLogState") String backLogState, @RequestParam("backLogID") String backLogID) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        String userID = userInfo.getUserID();
        int number = 0;
        try{
            number = backLogService.updateBackLogEndDTTM(backLogState, backLogID,userID);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(number);
    }

    @RequestMapping(value = "doUpdateBackLogUser")
    public JsonResult doUpdateBackLogUser(@RequestParam("backLogID") String backLogID, @RequestParam("UserID") String UserID) {
        UserInfo userInfo= (UserInfo) this.getSession().getAttribute("userInfo");
        String updateUser = userInfo.getUserID();
        int number = 0;
        try{
            number = backLogService.updateBackLogUser(backLogID, UserID, updateUser);;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new JsonResult(number);
    }


    @RequestMapping(value = "sendWorkDataByUserID")
    public JsonResult sendWorkDataByUserID( @RequestParam("UserID") String UserID) {
        UserInfo userInfo = (UserInfo) this.getSession().getAttribute("userInfo");
        try{
            backLogService.sendWorkDataByUserID(UserID);
            logService.insertLog("推送用户("+UserID+")工作任务",userInfo.getUserName());
        }catch (Exception e){
            e.printStackTrace();
            logService.insertLog("推送用户("+UserID+")工作任务异常信息  "+e.getMessage(),userInfo.getUserName());
        }
        return new JsonResult();
    }









}
